package cn.wanda.bi.sunshine.platform.server.service.statistics;

import cn.wanda.bi.sunshine.platform.server.bean.mapper.PageRelationMapper;
import cn.wanda.bi.sunshine.platform.server.bean.mapper.PageReportMapper;
import cn.wanda.bi.sunshine.platform.server.bean.mapper.PageWigetConfigMapper;
import cn.wanda.bi.sunshine.platform.server.bean.model.*;
import cn.wanda.bi.sunshine.platform.server.controller.request.PageRequest;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.SimpleResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wangfupeng1
 */
@Service
public class PageService {

	public static final Logger logger = LoggerFactory.getLogger(PageService.class);

	@Autowired
	private PageReportMapper pageReportMapper;
	@Autowired
	private PageWigetConfigMapper pageWigetConfigMapper;
	@Autowired
	private PageRelationMapper pageRelationMapper;

	// 增加
	public SimpleResponse<PageRequest> addPage(PageRequest pageRequest) {
		PageReport pageReport = pageRequest.getPageReport();
		pageReport.setStatus(1);
		pageReportMapper.insertSelective(pageReport);
		List<PageWigetConfig> pageWigetConfigs = pageRequest.getPageWigetConfig();
		if (pageWigetConfigs.size() != 0) {
			for (PageWigetConfig pageWigetConfig : pageWigetConfigs) {
				pageWigetConfigMapper.insertSelective(pageWigetConfig);
				PageRelation pageRelation = new PageRelation();
				pageRelation.setPid(pageReport.getId());
				pageRelation.setWid(pageWigetConfig.getWid());
				pageRelation.setwUuid(pageWigetConfig.getUuid());
				pageRelationMapper.insertSelective(pageRelation);
			}
		}
		return SimpleResponse.success(pageRequest);
	}

	// 根据PageId进行查询
	@Cacheable(value="cache:dmppage", key = "'cache:dmppage:page:id:'+#id")
	public SimpleResponse<PageRequest> queryPageById(Integer id) {
		PageReport pageReport = pageReportMapper.selectByPrimaryKey(id);
		PageRelationExample pageRelationExample = new PageRelationExample();
		pageRelationExample.createCriteria().andPidEqualTo(id);
		pageRelationExample.setOrderByClause("id desc");
		List<PageRelation> pageRelations = pageRelationMapper.selectByExample(pageRelationExample);
		ArrayList<PageWigetConfig> pageWigetConfigss = new ArrayList<>();
		for (PageRelation pageRelation : pageRelations) {
			Integer wid = pageRelation.getWid();
			String uuid = pageRelation.getwUuid();
			PageWigetConfigExample pageWigetConfigExample = new PageWigetConfigExample();
			pageWigetConfigExample.createCriteria().andWidEqualTo(wid).andUuidEqualTo(uuid);
			List<PageWigetConfig> pageWigetConfigs = pageWigetConfigMapper.selectByExample(pageWigetConfigExample);
			if (pageWigetConfigs.size() != 0) {
				PageWigetConfig pageWigetConfig = pageWigetConfigs.get(0);
				pageWigetConfigss.add(pageWigetConfig);
			}
		}
		PageRequest pageRequest = new PageRequest();
		pageRequest.setPageReport(pageReport);
		pageRequest.setPageWigetConfig(pageWigetConfigss);
		return SimpleResponse.success(pageRequest);
	}

	// 根据page id进行修改
	@CacheEvict(value="cache:dmppage", key = "'cache:dmppage:page:'+#pageRequest.getPageReport().getId()")
	public SimpleResponse<PageRequest> updatePage(PageRequest pageRequest) {
		pageReportMapper.updateByPrimaryKeySelective(pageRequest.getPageReport());
		PageRelationExample pageRelationExample = new PageRelationExample();
		pageRelationExample.createCriteria().andPidEqualTo(pageRequest.getPageReport().getId());
		List<PageRelation> pageRelations = pageRelationMapper.selectByExample(pageRelationExample);
		pageRelationMapper.deleteByExample(pageRelationExample);
		for (PageRelation pageRelation : pageRelations) {
			Integer wid = pageRelation.getWid();
			String uuid = pageRelation.getwUuid();
			PageWigetConfigExample pageWigetConfigExample = new PageWigetConfigExample();
			pageWigetConfigExample.createCriteria().andWidEqualTo(wid).andUuidEqualTo(uuid);
			pageWigetConfigMapper.deleteByExample(pageWigetConfigExample);
		}
		List<PageWigetConfig> pageWigetConfigs = pageRequest.getPageWigetConfig();
		for (PageWigetConfig pageWigetConfig : pageWigetConfigs) {
			pageWigetConfigMapper.insertSelective(pageWigetConfig);
			PageRelation pageRelation = new PageRelation();
			pageRelation.setPid(pageRequest.getPageReport().getId());
			pageRelation.setWid(pageWigetConfig.getWid());
			pageRelation.setwUuid(pageWigetConfig.getUuid());
			pageRelationMapper.insert(pageRelation);
		}
		return SimpleResponse.success(pageRequest);
	}

	// 删除page
	@CacheEvict(value="cache:dmppage", key = "'cache:dmppage:page:'+#id")
	public SimpleResponse<PageReport> deletePage(Integer id) {
		PageReport pageReport = pageReportMapper.selectByPrimaryKey(id);
		PageRelationExample pageRelationExample = new PageRelationExample();
		pageRelationExample.createCriteria().andPidEqualTo(id);
		List<PageRelation> pageRelations = pageRelationMapper.selectByExample(pageRelationExample);
		pageRelationMapper.deleteByExample(pageRelationExample);
		if (pageRelations.size() != 0) {
			for (PageRelation pageRelation : pageRelations) {
				Integer wid = pageRelation.getWid();
				String uuid = pageRelation.getwUuid();
				PageWigetConfigExample pageWigetConfigExample = new PageWigetConfigExample();
				pageWigetConfigExample.createCriteria().andWidEqualTo(wid).andUuidEqualTo(uuid);
				pageWigetConfigMapper.deleteByExample(pageWigetConfigExample);
			}
		}
		pageReportMapper.deleteByPrimaryKey(id);
		return SimpleResponse.success(pageReport);
	}

	// 查询所有Page
    public ListResponse<PageReport> queryPageName(String pageName,Integer index,Integer limit) {
		PageReportExample pageReportExample = new PageReportExample();
		PageReportExample.Criteria criteria = pageReportExample.createCriteria();
		if(pageName != null)
			criteria.andPNameLike("%"+pageName+"%");
		pageReportExample.setOffset(index * limit - limit);
		pageReportExample.setLimit(limit);
		pageReportExample.setOrderByClause("update_time desc");
		List<PageReport> pageReports = pageReportMapper.selectByExample(pageReportExample);
		int count = pageReportMapper.countByExample(pageReportExample);
		return ListResponse.success(pageReports,count,index, limit);
	}
}
